<?php
     /*
      * Developed by: Dionysios Fryganas under the GNU license
      */
      
     /*
      * @parameter: a string, representing the IBAN code
      * @returns: true if valid IBAN, false otherwise
      * // valid IBAN: GR15 0172 0700 0050 7005 1443 043 | GR7401710160006016010081870
      */    
    function isValid_IBAN($iban)
    {
        // replace the string according to the universal IBAN pattern
        $iban = preg_replace('#/^GR[0-9]{2}[0-9]{7}[A-Z0-9]{16}$/#', '', $iban);
        
        // invalid if length is less that 27
        if (strlen($iban) <> 27)
            return false;
        
        // declare the substitution values for the letters
        $LETTERS_TABLE = array(
            "A" => 10, "B" => 11,
            "C" => 12, "D" => 13,
            "E" => 14, "F" => 15,
            "G" => 16, "H" => 17,
            "I" => 18, "J" => 19,
            "K" => 20, "L" => 21,
            "M" => 22, "N" => 23,
            "O" => 24, "P" => 25,
            "Q" => 26, "R" => 27,
            "S" => 28, "T" => 29,
            "U" => 30, "V" => 31,
            "W" => 32, "X" => 33,
            "Y" => 34, "Z" => 35
    );
    
        // substitute the first letter to digit
        $digit1 = $LETTERS_TABLE[strtoupper( $iban[0] )]; 
        // substitute the second letter to digit
        $digit2 = $LETTERS_TABLE[strtoupper( $iban[1] )];
        
        // The given check digits (3rd and 4th digit)
        $CHECK_DIGITS = (int) substr( $iban, 2, 2 );
        
        // Move the first 4 digits of the IBAN to the end of the number
        $new_iban = substr( $iban, 4 ).$digit1.$digit2.$CHECK_DIGITS;
                
        // The algorithm iterates 3 times
        for( $i = 0; $i < 4; $i++ )
        {
            // Take the first 9 digits of the new IBAN
            $divider = (double) substr($new_iban, 0, 9);
            // Divide them with nine and store the remainder
            $new_head = (double)$divider % 97;
            // replace the first nine digits you had with the remainder
            $new_iban = substr_replace($new_iban, (string)$new_head, 0, 9);
        }
        
        // After the algorithm, the remainder of a valid IBAN should be one (1)
        return  $new_head == 1 ;
    }

function isValid_AMKA($amka)
{
    // Valid AMKA: 49927398716
           
    // replace the unwanted characters with empty
    $amka = preg_replace('#/[0-9]{11}$/#', '', $amka);
    
    // Not valid if not the desired length
    if(strlen($amka) <> 11)
        return false;
    
    $sum = 0;
    // The algorithm runs the length of the amka
    // and iterates through every digit 
    for ($i=0; $i < strlen($amka); $i++) 
    {     
        if($i % 2 == 0) // if is not even
            $sum += (int) substr($amka, $i, 1); // add the digit as is to the $sum
        else // if is even
        {
            $squared = (string)((int)substr($amka, $i, 1)) *2; // square the number
            for ($l=0; $l < strlen($squared); $l++) // add seperately every digit of $squared 
                $sum += substr($squared, $l, 1);
        }
    }
        // if the remainder of $sum / 10 == 0, is a valid amka and returns true else false
    return  $sum % 10 == 0;
}

function isValid_AFM($afm)
    {
        // Valid AFM: 113568693 | 143478219 | 124356456 | 243254657
        $afm = (string)$afm;
        
        $sum = 0;
        for( $iDigit=1; $iDigit <= 8; $iDigit++ )
        {
            $sum +=  intval( substr($afm, $iDigit-1, 1) * pow( 2, (9-$iDigit) ) );
        }
        
        $result = $sum%11 == 10 ? 0 : $sum%11;  
        
        return  $result == intval( $afm[8] );
    }
    
?>